﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Xml;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;

namespace KeyDoor.Searcher
{
    public partial class mapView : PhoneApplicationPage
    {
        private string postcode;
        private float lat;
        private float lon;
        private string edu = "";

        private bool zoomOut = false;

        public mapView()
        {
            InitializeComponent();
        }

        public void drawMap()
        {
            try
            {
                var homeloc = new Pushpin();
                homeloc.Location = new System.Device.Location.GeoCoordinate(lat, lon);
                homeloc.Content = postcode;
                propertyMap.Children.Add(homeloc);

                GeoCoordinate center = new GeoCoordinate(lat, lon);

                propertyMap.SetView(center, 17);

                PageTitle.Text = postcode.ToUpper();

                if (edu != "")
                {
                    int i=0;
                    foreach (KeyDoor.Searcher.index.educationFormat educ in Lifestyle.Storage.education.mainResults)
                    {
                        KeyDoor.Searcher.index.educationFormat.establishment educE = Lifestyle.Storage.education.mainResultsSub2[i];
                        KeyDoor.Searcher.index.educationFormat.establishment_location_metadata educM = Lifestyle.Storage.education.mainResultsSub1[i];

                        var newSchool = new Pushpin();
                        newSchool.Location = new GeoCoordinate(Convert.ToDouble(educ.Latitude), Convert.ToDouble(educ.Longitude));
                        newSchool.Content = educE.establishment_name;
                        if (educM.sector != "notsure")
                        {
                            newSchool.Content += Environment.NewLine + educM.sector + " school";
                        }

                        propertyMap.Children.Add(newSchool);
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can't read location data, please make sure you are connected to the internet and not behind a proxy.", "ERROR", MessageBoxButton.OK);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            postcode = this.NavigationContext.QueryString["postcode"].ToString();
            lat = Convert.ToSingle(this.NavigationContext.QueryString["lat"].ToString());
            lon = Convert.ToSingle(this.NavigationContext.QueryString["lon"].ToString());
            try { edu = this.NavigationContext.QueryString["edu"].ToString(); }
            catch { }

            drawMap();
        }

        private void propertyMap_MapZoom(object sender, MapZoomEventArgs e)
        {
            if (propertyMap.TargetZoomLevel <= 15.128)
            {
                for (int i = 0; i < propertyMap.Children.Count(); i++)
                {
                    if (i == 0) { }
                    else
                    {
                        propertyMap.Children.RemoveAt(i);
                    }
                }
                zoomOut = true;
            }

            if (zoomOut)
            {
                if (propertyMap.TargetZoomLevel > 15.128)
                {
                    if (edu != "")
                    {
                        int i = 0;
                        foreach (KeyDoor.Searcher.index.educationFormat educ in Lifestyle.Storage.education.mainResults)
                        {
                            KeyDoor.Searcher.index.educationFormat.establishment educE = Lifestyle.Storage.education.mainResultsSub2[i];
                            KeyDoor.Searcher.index.educationFormat.establishment_location_metadata educM = Lifestyle.Storage.education.mainResultsSub1[i];

                            var newSchool = new Pushpin();
                            newSchool.Location = new GeoCoordinate(Convert.ToDouble(educ.Latitude), Convert.ToDouble(educ.Longitude));
                            newSchool.Content = educE.establishment_name;
                            if (educM.sector != "notsure")
                            {
                                newSchool.Content += Environment.NewLine + educM.sector + " school";
                            }

                            propertyMap.Children.Add(newSchool);
                            i++;
                        }
                    }
                    zoomOut = false;
                }
            }
        }
    }
}